#include "add_new.h"
#include "ui_add_new.h"

add_new::add_new(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::add_new)
{
    ui->setupUi(this);
    this->setWindowTitle("新增信息");
    setAttribute(Qt::WA_QuitOnClose,false);
    sql = QSqlDatabase::addDatabase("QSQLITE", "forth");
    sql.setDatabaseName("child.db");
    bool open_or_not = sql.open();
    if(open_or_not == true){
        qDebug() << "数据库连接成功";
    }
    else{
        qDebug() << "数据库连接失败";
    }

    connect(ui->pushButton_save, SIGNAL(clicked(bool)), this, SLOT(Save_info()));


}

void add_new::display_add(){
    set_menu();
    this->show();
}


void add_new::Save_info(){
    QString str = get_Str();
    if(str == "false"){
        QMessageBox::warning(this, "插入失败", "插入失败", "确认", "取消");
        return;
    }
    QSqlQuery query(sql);
    bool ok = query.exec(str);
    if(ok == true){
        QMessageBox::information(this, "插入成功", "插入成功", "确认", "退出");
    }
    else{
        QMessageBox::warning(this, "插入失败", "插入失败", "确认", "取消");
    }
    this->close();
}

void add_new::set_menu(){
    ui->comboBox_famliy->addItem(tr("核心家庭"));
    ui->comboBox_famliy->addItem(tr("主干家庭"));
    ui->comboBox_famliy->addItem(tr("联合家庭"));
    ui->comboBox_famliy->addItem(tr("单亲家庭"));
    ui->comboBox_famliy->addItem(tr("重组家庭"));
    ui->comboBox_famliy->addItem(tr("丁克家庭"));

    ui->comboBox_grade->addItem(tr("一年级"));
    ui->comboBox_grade->addItem(tr("二年级"));
    ui->comboBox_grade->addItem(tr("三年级"));
    ui->comboBox_grade->addItem(tr("四年级"));
    ui->comboBox_grade->addItem(tr("五年级"));
    ui->comboBox_grade->addItem(tr("六年级"));
    ui->comboBox_grade->addItem(tr("七年级"));
    ui->comboBox_grade->addItem(tr("八年级"));
    ui->comboBox_grade->addItem(tr("九年级"));

    QStringList w_list;
    w_list.append(tr("国家机关党群组织"));
    w_list.append(tr("专业技术人员"));
    w_list.append(tr("办事人员和有关人员"));
    w_list.append(tr("商业服务业人员"));
    w_list.append(tr("农林牧渔水利"));
    w_list.append(tr("生产运输"));
    w_list.append(tr("军人"));
    w_list.append(tr("不便分类"));
    w_list.append(tr("无职业"));

    QStringList e_list;
    e_list.append(tr("博士"));
    e_list.append(tr("硕士"));
    e_list.append(tr("本科"));
    e_list.append(tr("大专"));
    e_list.append(tr("中专与中技"));
    e_list.append(tr("高中"));
    e_list.append(tr("初中"));
    e_list.append(tr("小学"));
    e_list.append(tr("文盲与半文盲"));

    ui->comboBox_f_w->addItems(w_list);
    ui->comboBox_m_w->addItems(w_list);
    ui->comboBox_f_lv->addItems(e_list);
    ui->comboBox_m_lv->addItems(e_list);

    QButtonGroup btn_group;
    btn_group.addButton(ui->radioButton_female);
    btn_group.addButton(ui->radioButton_male);
    btn_group.setExclusive(true);
}

QString add_new::get_Str(){
    QString str;
    str = "INSERT INTO child_data(name, sex, birth, grade, phone, family, Father_edu_level, Mother_occ, Father_occ, Mother_occ) VALUES ";
    QString name = ui->lineEdit_name->text();
    QString sex;
    if(ui->radioButton_male->isChecked() == true){
        sex = "1";
    }
    else{
        sex = "0";
    }
    QString birth = ui->dateEdit_date->text();
    QString grade = QString::number(ui->comboBox_grade->currentIndex());
    QString phone = ui->lineEdit_phone->text();
    QString family = QString::number(ui->comboBox_famliy->currentIndex());
    QString Father_l = QString::number(ui->comboBox_f_lv->currentIndex());
    QString Mother_l = QString::number(ui->comboBox_m_lv->currentIndex());
    QString Father_w = QString::number(ui->comboBox_f_w->currentIndex());
    QString Mother_w = QString::number(ui->comboBox_m_w->currentIndex());
    if(birth.isEmpty() == true || phone.isEmpty() == true){
        QMessageBox::warning(this, "插入失败", "不能有空", "确认", "取消");
        return "false";
    }

    str += "('" + name + "',";
    str += sex + ",";
    str += "'" + birth + "',";
    str += grade + ",";
    str += "'" + phone + "',";
    str += family + ",";
    str += Father_l + ",";
    str += Mother_l + ",";
    str += Father_w + ",";
    str += Mother_w + ");";

    qDebug() << str;
    return str;
}

add_new::~add_new()
{
    delete ui;
}
